package create.SingletonPattern.practice;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * @功能职责: 单例模式 --- 恶汉模式
 * @描述：
 * @作者: 郭辉
 * @创建时间: 2020-12-02
 * @copyright Copyright (c) 2020 中国软件与技术服务股份有限公司
 * @company 中国软件与技术服务股份有限公司
 */
public class SingletonPatternEvil {
    //立即声明
    private static SingletonPatternEvil singleton = new SingletonPatternEvil();
    //私有化构造方法
    private SingletonPatternEvil(){
        if (SingletonPatternEvil.singleton != null){
            throw new RuntimeException("请勿恶意破坏");
        }
    }

    public static SingletonPatternEvil getInstance(){
        return singleton;
    }
}

class TestSingletonPatternEvile{
    public static void main(String[] args) throws Exception {

        Field singleton = SingletonPatternEvil.class.getDeclaredField("singleton");
        singleton.setAccessible(true);
        singleton.set(null,null);

        Constructor<SingletonPatternEvil> declaredConstructor = SingletonPatternEvil.class.getDeclaredConstructor(null);
        declaredConstructor.setAccessible(true);

        SingletonPatternEvil singletonPatternEvil2 = declaredConstructor.newInstance();
        SingletonPatternEvil singletonPatternEvil1 = declaredConstructor.newInstance();
        System.out.println(singletonPatternEvil1.hashCode());
        System.out.println(singletonPatternEvil2.hashCode());
    }
}
